const config = require("./config") 
// console.log("obj",config)
export async function linkWebSocket(handleOpen:EventListener):Promise<WebSocket>{
	// const socket:WebSocket = new WebSocket("ws://43.136.117.207:8999/GetAllDataWebSocket");
	const socket:WebSocket = new WebSocket("ws://43.136.117.207:8999/ActionSocket");
	if(!socket){
			uni.showToast({
				icon: "none",
				title: "无法连接服务器",
				duration: 4000
			})
		console.log("连接错误")
		throw Error("socket连接错误")
	}
	function init(){
		socket.addEventListener("open",handleOpen,false);
		socket.addEventListener("close",handleClose,false);
		socket.addEventListener("error",handleError,false);
		socket.addEventListener("message",handleMessage,false);
	}
	// return new Promise()
	function handleMessage(e:Event){
		// console.log('WebSocket open', e);
		console.log("返回信息了")
		
		 // const initmessage = [{},{}]
		 // socket.send(JSON.stringify(initmessage))
		 
	}
	function handleClose(e:Event){
		 // console.log('WebSocket close', e);

	}
	function handleError(e:Event){
		 console.log('WebSocket error', e);
	}
	
	init();
	console.log(socket)
	return socket;
}
// 处理
// export 